/**
 * 解析路径
 */
export function qsUrl(url = location.href) {
  const query:{ // 用一个对象存储目标值
    [key: string]: string
  } = {}
  const paramsArr:string[] = url.split('?') // 截取?号后的字符串即name=itclanCoder&study=css

  if (paramsArr.length > 1) {
    const params = paramsArr[ 1 ]

    params.split('&').forEach(item => {
      const param = item.split('=')

      query[ param[ 0 ] ] = param[ 1 ]
    })
  }
  return {
    fullPath: location.href,
    path: location.pathname,
    query
  }
}

/**
* 防抖
* @param {Function} cb 回调
* @param {Number} delay 多久后执行
*/
let dTimer:any

export function debounce(cb:Function, delay = 500) {
  if (dTimer) {
    clearTimeout(dTimer)
  }
  dTimer = setTimeout(() => {
    cb()
  }, delay)
}
/**
* 节流
* @param {Function} cb 回调
* @param {Number} time 多久执行一次
*/
let tTimer:any

export function throttle(cb:Function, time = 500) {
  if (!tTimer || Date.now() - tTimer >= time) {
    tTimer = Date.now()
    cb()
  }
}

/**
 * 根据id获取项目类型
 */
import { ProjectType } from '@/modules/project'
export function getProjectTypeById(id:number):ProjectType {
  if (id < 50000000) {
    return 'competition'
  }
  return 'courseDesign'
}
